<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Cat;

class CatController extends Controller
{

    protected $cat;
    public function __construct(){
        $this->Cat = new Cat();
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $cats = Cat::select('catid','parentid','catname')->get();
        // $cats = Cat::all();
        $cats = $this->Cat->findAll();
        // dd($cats);
        return view('admin.cat_index')->with('cats',$cats);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cats = $this->Cat->findAll();
        return view('admin.cat_add')->with('cats',$cats);
    }

    /**
     * Store a newly created resource in storage.
     *sql数据导入时字段属性
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $cat = $this->Cat;
        $cat->parentid = $request->parentid;
        $cat->catname = $request->catname;
        $cat->save();
        return redirect('admin/cat');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //子目录有问题
        $data = Cat::select('catid','parentid','catname')->get();
        $cats = $this->familytree2($data,$id,0);
        // dd($cats);
        return view('admin.cat_edit')->with('cats',$cats);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        Cat::where('id', $id)
            ->update($request->except('_token','_method'));
        return redirect('admin/cat');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        
    }


    //ajax删除有子栏目的不能删除
    public function del(Request $request){
        $id = $request['id'];
        return Cat::where('catid',$id)->delete();  
    }


    // /**
    //  * 找子孙栏目 前后台都使用 放在catmodel中
    //  * @param $data  栏目数据源
    //  * @param int $id
    //  * @param int $lev
    //  * @return array
    //  */
    // protected  function  findTree($data,$id=0,$lev=0)
    // {
    //     static $tree=[];
    //     foreach($data as $k=>$v){
    //         if($v->parentid==$id){
    //             $v->lev=$lev;
    //             $tree[] = $v;
    //             $this->findTree($data,$v->catid,$lev+1);
    //         }
    //     }
    //     return $tree;
    // }
    // //格式化取出所有栏目
    // protected function findAll()
    // {
    //     $data = Cat::select('catid','parentid','catname')->get();
    //     return  $this->findTree($data,0,0);
    // }


    //家朴树
    function familytree2($data,$id,$lev=1)
    {
        static $tree=[];
        foreach($data as $v){
            if($v->catid == $id){
                $v->lev = $lev;
                $tree[]=$v;
                if($v->parentid>0){
                    $this->familytree2($data,$v->parentid,$lev+1);
                }
            }
        }
        return array_reverse($tree);
    }

    
    //找父元素
    function findParent($data,$pid)
    {
        $parent=[];
        foreach ($data as $v) {
            if($v['catid']==$pid){
                $parent[]=$v;
            }
        }
        return $parent;
    }


}
